package com.atguigu.spzx.manager.task;


import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;

@Component
public class OrderStatisticsTask {
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;
//    @Scheduled(cron = "0/5 * * * * ?")
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics() {
        System.out.println(new Date().toInstant());
       // 1.获取前一天日期
        String createTime = DateUtil.offsetDay(new Date(), -1).toString(new SimpleDateFormat("yyyy-MM-dd"));
      // 2.根据前一天日期进行统计功能(分组操作)
        // 统计前一天交易金额
        OrderStatistics orderStatistics = orderInfoMapper.selectOrderStatistics(createTime);

     // 把统计之后的数据 添加统计结果表里面
        if (orderStatistics != null) {
            orderStatisticsMapper.insert(orderStatistics) ;
        }
    }
}
